using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using IfacesEnumsStructsClasses;
using csExWB;
using System.Diagnostics;

namespace ConsoleApp
{    
    class Program
    {
        private static bool done = false;
        private static string pagesource = string.Empty;

        /*


We need to add [STAThread] attribute to the Main

Webbrowser control is a single threaded COM object and does not work
properly when used in a multithreaded apartment model (MTA).
Internally, it causes exceptions such as:

An outgoing call cannot be made since the application is dispatching
an input-synchronous call. (when IOleObject::SetClientSite is called,
MSHTML attempts to call IOleInPlaceSite::GetWindow before returnning
from the initial call)

and

Cross-thread operation not valid (When IOleInPlaceSite::GetWindow is
called while an initial call to IOleObject::SetClientSite has not
returned)

and a few more InvalidOperationException exceptions

which prevents webbrowser control from performing as it normally does
under a STA thread model.
         
         */

        [STAThread]
        static void Main(string[] args)
        {
            //init browser 
            csExWB.cEXWB pWb = new cEXWB();
            pWb.Size = new System.Drawing.Size(800, 600);
            pWb.WBDOCDOWNLOADCTLFLAG = (int)
                (DOCDOWNLOADCTLFLAG.NO_SCRIPTS |
                 DOCDOWNLOADCTLFLAG.NO_DLACTIVEXCTLS |
                 DOCDOWNLOADCTLFLAG.NO_JAVA |
                 DOCDOWNLOADCTLFLAG.NO_RUNACTIVEXCTLS |
                 DOCDOWNLOADCTLFLAG.PRAGMA_NO_CACHE |
                 DOCDOWNLOADCTLFLAG.SILENT);
            pWb.DownloadSounds = false;
            pWb.DownloadVideo = false;

            pWb.DocumentComplete += new DocumentCompleteEventHandler(pWb_DocumentComplete);
            pWb.NavigateError += new NavigateErrorEventHandler(pWb_NavigateError);
            pWb.WBSecurityProblem += new SecurityProblemEventHandler(pWb_WBSecurityProblem);
            pWb.BeforeNavigate2 += new BeforeNavigate2EventHandler(pWb_BeforeNavigate2);

            pWb.NavToBlank();
            
            pWb.Navigate("http://www.google.com");
            while (!done)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(100);
            }

            //Get the source, the document, ....
        }

        static void pWb_BeforeNavigate2(object sender, BeforeNavigate2EventArgs e)
        {
            Debug.Print("BeforeNavigate2==>" + e.url + " =istoplevel===> " + e.istoplevel.ToString());
        }

        static void pWb_DocumentComplete(object sender, DocumentCompleteEventArgs e)
        {
            if (e.url.ToLower() == "about:blank")
            {
                Debug.Print("DocumentComplete::about:blank = istoplevel===>" + e.istoplevel.ToString());
                return;
            }
            if (e.istoplevel)
            {
                done = true;
                Debug.Print("DocumentComplete::TopLevel is TRUE===>" + e.url);
            }
            else
            {
                Debug.Print("DocumentComplete::TopLevel is FALSE===>" + e.url);
            }
        }

        static void pWb_WBSecurityProblem(object sender, SecurityProblemEventArgs e)
        {
            Debug.Print("WBSecurityProblem==>" + e.problem.ToString());
        }

        static void pWb_NavigateError(object sender, NavigateErrorEventArgs e)
        {
            Debug.Print("NavigateError==>" + e.statuscode.ToString());
            done = true;
        }
    }
}
